package repository

import (
	"fmt"
	"github.com/gccai/write_helper/model"
	"github.com/gccai/write_helper/query"
	"github.com/gccai/write_helper/utils"
	"github.com/jinzhu/gorm"
)

type UserRepository struct {
	DB *gorm.DB
}

// 增，删，改，查
type UserRepositoryInterface interface {
	// 查看所有的用户
	List(req *query.ListQuery) (users []*model.User, err error)
	// 获取用户总数
	GetTotal(req *query.ListQuery) (total int, err error)
	// 获取某个用户的信息
	Get(user model.User) (*model.User, error)
	// 查寻用户、查找某个用户
	Exist(user model.User) *model.User
	// 按照 id 查找用户
	ExistByUserID(id string) *model.User
	// 按照手机号查找用户
	ExistbyMobile(mobile string) *model.User
	// 添加用户
	Add(user model.User) (*model.User, error)
	// 更新
	Edit(user model.User) (bool, error)
	// 删除
	Delete(user model.User) (bool, error)
}

func (u UserRepository) List(req *query.ListQuery) (users []*model.User, err error) {
	db := u.DB
	limit, offset := utils.Page(req.PageSize, req.Page)
	if err := db.Where("is_deleted = ?", false).Order("user_id desc").Limit(limit).Offset(offset).Find(&users).Error; err != nil {
		return nil, err
	}
	return users, nil

}

func (u UserRepository) GetTotal(req *query.ListQuery) (total int, err error) {
	var users []model.User
	db := u.DB
	if err := db.Where("is_deleted = ?", false).Find(&users).Count(&total).Error; err != nil {
		return total, err
	}
	return total, nil
}

func (u UserRepository) Get(user model.User) (*model.User, error) {
	if err := u.DB.Where(&user).Where("is_deleted = ?", false).Find(&user).Error; err != nil {
		return nil, err
	}
	return &user, nil
}

func (u UserRepository) Exist(user model.User) *model.User {
	// 我这里的检查最后查找是否成功与原代码是不一样的
	//res := u.DB.Find(&user).Where("nick_name=?", user.NickName)
	var User model.User
	res := u.DB.Where("user_id = ? and is_deleted = ?", user.UserId, false).Find(&user)
	if res.RowsAffected > 0 {
		return &User
	}
	return nil
}

func (u UserRepository) ExistByUserID(id string) *model.User {
	var user model.User
	//res := u.DB.Find(&user).Where("user_id=?", id)
	res := u.DB.Where("user_id = ? and is_deleted = ?", id, false).Find(&user)
	if res.RowsAffected > 0 {
		return &user
	}
	return nil
}

func (u UserRepository) ExistbyMobile(mobile string) *model.User {
	var user model.User
	fmt.Println(mobile)
	res := u.DB.Where("mobile = ? and is_deleted = ?", mobile, false).Find(&user)
	//res := u.DB.Find(&user).Where("mobile=?", mobile)
	if res.RowsAffected > 0 {
		return &user
	}
	return nil
}

func (u UserRepository) Add(user model.User) (*model.User, error) {
	if exist := u.ExistbyMobile(user.Mobile); exist != nil {
		return nil, fmt.Errorf("用户已经存在")
	}
	err := u.DB.Create(&user).Error
	if err != nil {
		return nil, fmt.Errorf("用户注册失败")
	}
	return &user, nil
}

func (u UserRepository) Edit(user model.User) (bool, error) {
	fmt.Println(user)
	if err := u.DB.Model(&user).Where("user_id = ?", user.UserId).Updates(map[string]interface{}{
		"nick_name": user.NickName,
		"mobile":    user.Mobile,
		"address":   user.Address,
	}).Error; err != nil {
		return false, err
	}
	return true, nil
}

func (u UserRepository) Delete(user model.User) (bool, error) {

	err := u.DB.Model(&user).Where("user_id = ?", user.UserId).Update(map[string]interface{}{
		"is_deleted": user.IsDeleted,
	}).Error
	if err != nil {
		return false, err
	}
	return true, nil
}
